package com.Day8.mystack;

/**
 * Copyright (C), 2018-2021, 若若叶
 * Author: Hx162
 * Date: 2021/7/13 13:27
 * FileName: MyStack
 * Description: 模拟 栈
 */

/**
 * 使用异常重写栈
 * 模拟一个栈
 * 有入栈，出栈，遍历栈三个功能
 * 栈满，栈空 提示
 * 栈满不得入栈
 * 栈空不得出栈
 */
public class MyStack {
    static Object[] stack = new Object[10];
    static int pointer = 0;

    public static void push(int dser) throws StackException {
        if (pointer < 10) {
            stack[pointer] = dser;
            if (pointer == 10) {
                System.out.println("栈满...");
            }
            pointer++;
        } else {
            throw new StackException("栈满，无法入栈...");
        }
    }

    public static void pop() throws StackException {
        if (pointer > 0) {
            pointer--;
            stack[pointer] = null;
            if (pointer == 0) {
                System.out.println("栈空...");
            }
        } else {
            throw new StackException("栈已空，无发出栈...");
        }
    }

    public static void print(Object[] o) {
        for (int i = 0; i < o.length; i++) {
            System.out.print(o[i] + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {

//        try {
//            push(12);
//            push(13);
//            push(14);
//            push(15);
//            push(16);
//            push(16);
//            push(16);
//            push(16);
//            push(16);
//            push(17);
//            push(16);
//        } catch (StackException e) {
//            //e.printStackTrace();
//            System.out.println(e.getMessage());
//        }
//        print(stack);
        try {
            pop();
            pop();
            pop();
        } catch (StackException e) {
            //e.printStackTrace();
            System.out.println(e.getMessage());
        }
        print(stack);
    }
}
